home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Language/OS - Multiplatform Resource Library
/
LANGUAGE OS.iso
/
smaltalk
/
manchest.lha
/
MANCHESTER
/
manchester
/
2.2
/
MVC-Wires.st
< prev
next >
Wrap
Text File
|
1993-07-24
|
6KB
|
269 lines
" NAME MVC-Wires
AUTHOR ikp@cs.man.ac.uk
FUNCTION an MVC tutorial e.g.
ST-VERSIONS 2.2
PREREQUISITES
CONFLICTS
DISTRIBUTION world
VERSION 1.1
DATE 22 Jan 1989
SUMMARY MVC-Wires
is the simple MVC example presented in Trevor's Tektronix
notes. The omissions and embelishments mentioned in the notes have
been implemented, plus a few extras such as a yellow-button menu
which modifies the action performed by the red button.(2.2).IKP
"!
OrderedCollection variableSubclass: #Wire
instanceVariableNames: ''
classVariableNames: ''
poolDictionaries: ''
category: 'MVC-Wires'!
!Wire methodsFor: 'accessing'!
length
"answer the length of the wire"
| total first next |
total _ 0.
self size > 0
ifTrue:
[first _ self first.
self do:
[:next |
total _ total + (first dist: next).
first _ next]].
^total!
pinNear: aPoint
"answer the index of the pin nearest aPoint"
| bestDistance bestIndex pin |
bestIndex _ 1.
self size > 2
ifTrue:
[bestDistance _ aPoint dist: (self at: 1).
1 to: self size do:
[:index |
(aPoint dist: (pin _ self at: index)) < bestDistance
ifTrue:
[bestDistance _ aPoint dist: pin.
bestIndex _ index]]].
^bestIndex!
randomIndex
"return an index for a randomly chosen pin"
| aRandom |
aRandom _ Random new.
^(aRandom next * self size) asInteger + 1! !
!Wire methodsFor: 'manipulating'!
shorten
"Make random changes to routing order. Keep only those changes that
shorten the length."
| i j minLength |
self size > 2
ifTrue:
[minLength _ self length.
i _ self randomIndex.
j _ self randomIndex.
self swap: i with: j.
self length < minLength
ifTrue:
[minLength _ self length.
self changed]
ifFalse: [self swap: i with: j]]! !
MouseMenuController subclass: #WireController
instanceVariableNames: 'modeMessage '
classVariableNames: 'WireYellowButtonMenu WireYellowButtonMessage '
poolDictionaries: ''
category: 'MVC-Wires'!
!WireController methodsFor: 'initialize'!
initialize
"perform controller instance initialization, then setup yellow button menu"
super initialize.
self initializeYellowButtonMenu.
self modeMessage: #addPin!
initializeYellowButtonMenu
"initialize yellow button activities from class defaults"
self yellowButtonMenu: WireYellowButtonMenu
yellowButtonMessages: WireYellowButtonMessage! !
!WireController methodsFor: 'control defaults'!
controlActivity
sensor noButtonPressed
ifTrue: [model shorten].
super controlActivity!
isControlActive
"control is active provided the cursor is in our view and we don't want the blue button menu"
^(view containsPoint: sensor cursorPoint) & sensor blueButtonPressed not!
isControlWanted
"control is wanted provided we don't want the blue button menu"
^self sensor blueButtonPressed not and: [super isControlWanted]! !
!WireController methodsFor: 'menu messages'!
addPin
"add pin to wire"
self model add: (view inverseDisplayTransform: sensor waitNoButton).
model changed!
movePin
"move an existing point in wire"
| old new index |
model size > 0
ifTrue:
[index _ model pinNear: (old _ self view inverseDisplayTransform: sensor waitButton).
sensor cursorPoint: (view displayTransform: (model at: index)).
new _ view inverseDisplayTransform: sensor waitNoButton.
model at: index put: new.
model changed]!
redButtonActivity
"perform the appropriate red button activity"
self view topView isCollapsed
ifFalse: [self perform: modeMessage]!
removePin
"remove pin from wire"
self model size > 0
ifTrue:
[sensor cursorPoint:
(view displayTransform:
(model at: (model pinNear: (view inverseDisplayTransform: sensor cursorPoint)))).
model removeAtIndex: (model pinNear: (view inverseDisplayTransform: sensor waitNoButton)).
model changed]!
selectAddPin
"select add mode"
self view topView newLabel: 'add pin'.
self modeMessage: #addPin!
selectMovePin
"select move mode"
self view topView newLabel: 'move pin'.
self modeMessage: #movePin!
selectRemovePin
"select remove mode"
self view topView newLabel: 'remove pin'.
self modeMessage: #removePin! !
!WireController methodsFor: 'private'!
modeMessage: aSymbol
"set this controllers mode message to aSymbol"
modeMessage _ aSymbol! !
"-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- "!
WireController class
instanceVariableNames: ''!
!WireController class methodsFor: 'class initialization'!
initialize
"WireController initialize."
WireYellowButtonMenu _ PopUpMenu labels: 'add\move\remove' withCRs.
WireYellowButtonMessage _ #(selectAddPin selectMovePin selectRemovePin)! !
WireController initialize!
View subclass: #WireView
instanceVariableNames: 'pen '
classVariableNames: ''
poolDictionaries: ''
category: 'MVC-Wires'!
!WireView methodsFor: 'initialize-release'!
initialize
"perform default view initialization, and initialize our very own pet pen"
super initialize.
pen _ Pen new up.
pen combinationRule: Form over! !
!WireView methodsFor: 'updating'!
update: nilParameter
"my model has changed - redraw the view"
self display! !
!WireView methodsFor: 'displaying'!
displayView
"display a wire as a single pixel line."
self topView isCollapsed
ifFalse:
[self clearInside.
model do: [:pin | pen goto: (self displayTransform: pin); down].
pen up]! !
!WireView methodsFor: 'controller access'!
defaultControllerClass
"what I'd like my controller to be, given the choice"
^WireController! !
"-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- "!
WireView class
instanceVariableNames: ''!
!WireView class methodsFor: 'instance creation'!
open
"build and return a WireView onto an empty wire"
^self openOn: Wire new!
openOn: aWire
"build and return a WireView on an existing wire"
| topView aView |
topView _ StandardSystemView model: nil label: 'add pin' minimumSize: 100@100.
aView _ self new borderWidth: 1.
aView insideColor: Form white.
aView model: aWire.
topView addSubView: aView.
topView controller open! !
!WireView class methodsFor: 'example'!
example
"accept a wire from the user and a viewport to display it in."
"WireView example"
WireView openOn: Wire new! !